Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add filters to prompt function #1371

Merged
merged 5 commits into from
Jan 14, 2025
Merged

Conversation

derfred
Copy link
Contributor

@derfred derfred commented Jan 10, 2025

Allow giving custom filters to the prompt decorator

def reverses: str) -> str:
    return s[::-1]

@prompt(filters={ 'reverse': reverse })
def reverse_prompt(text):
    '''{{ text | reverse }}'''

prompt = reverse_prompt("Hello")

print(prompt)
>>> "olleH"

return env.get_template(os.path.basename(path))


def prompt(fn: Callable) -> Prompt:
def prompt(fn: Optional[Callable] = None, **filters: Dict[str, Callable]) -> Callable:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filters should be a simple argument that has type Dict[str, Callable] and maps the filter names to the functions.

@@ -118,10 +125,14 @@ def _template_from_file(_, path: Path) -> jinja2.Template:
keep_trailing_newline=True,
undefined=jinja2.StrictUndefined,
)

for name, filter_fn in filters.items():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally don't mind a little repetition but I think we should refactor the code and add a create_environment class that can be used in both class methods. @yvan-sraka are there any reasons we shouldn't do that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, ideally, it would be less surprising for users to have the most similar environment (and behavior) for any method used to create a Prompt. Otherwise, it's a great feature addition!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a create_jinja_env function.

@derfred
Copy link
Contributor Author

derfred commented Jan 11, 2025

thanks for your review @rlouf. I've taken your comments into account. While doing so the following alternative approach came to me:

env = outlines.prompts.prompt_environment([reverse])

@env
def my_prompt(text):
  "reversed: {{ text | reverse }}"

what do you think?

@rlouf
Copy link
Member

rlouf commented Jan 12, 2025

Thank you for being attentive to the general design! I think your original design to be simpler, we should go with it (including my comments).

@derfred
Copy link
Contributor Author

derfred commented Jan 13, 2025

ok, great! Do you have any more comments about the implementation?

Comment on lines 210 to 212
if isinstance(filters, list):
for filter_fn in filters:
env.filters[filter_fn.__name__] = filter_fn
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's stick to filters being a dictionary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the list option.

@rlouf
Copy link
Member

rlouf commented Jan 14, 2025

Looks good to me! @yvan-sraka ?

Copy link
Contributor

@yvan-sraka yvan-sraka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Great contribution, thanks :)

@rlouf rlouf merged commit 79100b2 into dottxt-ai:main Jan 14, 2025
@derfred
Copy link
Contributor Author

derfred commented Jan 14, 2025

Awesome!

@derfred derfred deleted the custom_prompt_filters branch January 14, 2025 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants